package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.rainf.service.IClassInfoService;
import top.rainf.support.Result;
import top.rainf.domain.vo.ClassInfo;

import java.util.List;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description 分类信息的控制器类 由本人的生成器自动生成~
 */
@RestController
@RequestMapping("/classInfo")
public class ClassInfoController {
    @Autowired
    IClassInfoService classInfoService;
    /**
    * 获取全部分类信息的列表
    * @return 返回是否获取成功~
    */
    @RequestMapping("/list")
    public Result list(){
        List<ClassInfo> list = classInfoService.getClassInfoList();
        if(list == null){
            return Result.fail();
        }
        return Result.success().addObj("list",list);
    }

    /**
     * 获取某板块的全部分类信息
     * @param id
     * @return
     */
    @RequestMapping("/list-block")
    public Result listBlock(int id){
        List<ClassInfo> list = classInfoService.getClassInfoListByBlockId(id);
        return Result.success().addObj("list",list);
    }

    /**
    * 根据id获取一个分类信息
    * @return 返回是否获取成功~
    */
    @RequestMapping("/one")
    public Result getOneClassInfo(int id){
        ClassInfo classInfo = classInfoService.getOneById(id);
        if(classInfo == null){
                    return Result.fail();
        }
        return Result.success().addObj("data",classInfo);
    }
    /**
    * 根据id删除一个分类信息
    ---------------------------
    * @param id 要删除的分类信息的id
    * @return 返回是否获取成功~
    */
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        boolean suc = classInfoService.deleteOneById(id);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(List<Integer> idList){
        boolean suc = classInfoService.deleteAll(idList);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("update")
    public Result update(ClassInfo classInfo){
        boolean suc = classInfoService.updateById(classInfo);
        return suc ? Result.success() : Result.fail();
    }
}
